# -*- coding:utf-8 -*-
from lafeier import Scraper, HtmlPage
from mymodule.config import getrdc


class AISpider:
    # 从本地文件训练
    # 参数：
    # [
    # 	{
    # 		"filename":"文件名",
    # 		"data":{
    # 			"字段名":值,
    # 			"字段名":值
    # 		}
    # 	}
    # 	……多个
    # ]
    @classmethod
    def trainFromLocalFiles(cls, items, tplName,saveFilename="", encoding='utf-8',cnTplName=""):

        s = Scraper()
        for item in items:
            file = open(item["filename"])
            content = file.read()
            page = HtmlPage(body=unicode(content.decode(encoding)), encoding=encoding)
            s.train_from_htmlpage(page, item["data"])
        if (saveFilename != ""):
            file = open(saveFilename, "wb")
            s.tofile(file)
            dic = {}
            dic['cnTplName'] = cnTplName
            dic['tplname'] = tplName
            r=getrdc()
            r.lpush('tplname', dic)
        return s

    # 从网址训练
    # 参数
    # [
    # 	{
    # 		"url":"网址",
    # 		"data":{
    # 			"字段名":值,
    # 			"字段名":值
    # 		}
    # 	}
    # 	……多个
    # ]
    @classmethod
    def trainFromUrls(cls, items, saveFilename="", encoding='utf-8'):
        s = Scraper()
        for item in items:
            s.train(item["url"], item["data"], encoding=encoding)
        if (saveFilename != ""):
            file = open(saveFilename, "wb")
            s.tofile(file)
        return s

    # 从以存在的模板获取一个Scrapely实例
    # 参数：文件名[必须]
    @classmethod
    def getScrapelyFromTpl(cls, filename):
        file = open(filename, "rb")
        return Scraper.fromfile(file)

    # 将本地网页转成Page对象
    @classmethod
    def fileToPage(csl, filename, encoding='utf-8'):
        file = open(filename)
        content = file.read()
        return HtmlPage(body=unicode(content.decode(encoding)), encoding=encoding)